package org.clockwise.feature;

import org.clockwise.nlpir.ResultParser;
import org.clockwise.nlpir.TestNLPIR;

/**
 * This class stores the information about a labeled keyword, 
 * including the key word, type, linked id, and the position in
 * the original unlabeled text.
 * 
 * @author WangYan
 *
 */

public class TextLabel {
	
	public static final String PERSON_LABEL = "per";
	public static final String LOCATION_LABEL = "loc";
	public static final String ORGAN_LABEL = "org";
	public static final String ID_LABEL = "id=";
	public static final int ID_START_POS = ID_LABEL.length();
	
	public String key = null;			// the key word that be labeled
	public String labelType = null;		// the type of the label: person, location or organization
	public boolean isLinked = false;	// whether the key word has a link
	public Long linkId = null;			// the linked id, if there is one
	public int beginPos = -1;			// the start position in an unlabeled text
	
	public String[] segWords;			// the NLPIR result : words
	public String[] segLabels;			// the NLPIR result : labels
	
	public Query query;
	
	public TextLabel(String line) {
		String[] seg = line.split("#");
		if (seg.length != 2) {
			System.out.println("Error! Illegal line to create TextLabel from format input");
			System.out.println(line);
		}
		else {
			String[] info = seg[0].split("\t");
			if (info.length != 5) {
				System.out.println("Fatal Error! Incomplete info to construct TextLabel");
				System.out.println(line);
			}
			else try {
				key = info[0];
				labelType = info[1];
				isLinked = Boolean.parseBoolean(info[2]);
				if (info[3].equals("null")) {
					linkId = null;
				}
				else {
					linkId = Long.parseLong(info[3]);
				}
				beginPos = Integer.parseInt(info[4]);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			String[][] result = ResultParser.parse(seg[1]);
			segWords = result[0];
			segLabels = result[1];
		}
	}
	
	// format: key labelType isLinked linkId beginPos#segWord[0]/segLabels[0] ...
	public String getRecordString() {
		String separator = "\t";
		StringBuilder sb = new StringBuilder();
//		if (key.contains(separator) || labelType.contains(separator)) {
//			System.out.println("Warning: there is separator in keyword");
//		}
		sb.append(key);
		sb.append(separator);
		sb.append(labelType);
		sb.append(separator);
		sb.append(isLinked);
		sb.append(separator);
		sb.append(linkId);
		sb.append(separator);
		sb.append(beginPos);
		sb.append("#");
		for (int i = 0; i < segWords.length; i++) {
			sb.append(segWords[i]);
			sb.append("/");
			sb.append(segLabels[i]);
			if (i != segWords.length - 1) {
				sb.append(" ");
			}
		}
		return sb.toString();
	}
	
	
	
	public TextLabel(String label, String keyword, int pos) {
		this.key = keyword.toLowerCase().trim();
		this.beginPos = pos;
		String result = TestNLPIR.test(key);
		String[][] parseResult = ResultParser.parse(result);
		segWords = parseResult[0];
		segLabels = parseResult[1];
//		System.out.println(label);
		
		if (label.contains(ID_LABEL)) {
			String[] pair = label.split(" ");
			if (pair.length == 2) {
				labelType = pair[0];
				String id = pair[1].substring(ID_START_POS, pair[1].length());
				try {
					linkId = Long.parseLong(id);
					isLinked = true;
				} catch (NumberFormatException e) {
					System.out.println("Error: illegal id: " + id);
				}
			}
			else {
				System.out.println("Error: illegal label format: " + label);
			}
		}
		else {
			labelType = label;
		}
	}
	
	
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Keyword: ");
		sb.append(key);
		sb.append(", ");
		sb.append("type: ");
		sb.append(labelType);
		sb.append(", ");
		
		if (isLinked) {
			sb.append("link: ");
			sb.append(linkId.toString());
		}
		else {
			sb.append("no link");
		}
		
		return sb.toString();
	}
	
	
}
